﻿/*
 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

#include "PortManager.h"
#include "SerialPort.h"
#include "TcpPort.h"
using namespace Base;
using namespace SerialPortClass;
PortManager::PortManager()
{
    manager[SERIAL_PORT] = new SerialPort;
    manager[TCP_PORT] = new TcpPort();
}

PortManager::~PortManager()
{
    if (manager[0] != nullptr) {
        delete manager[0];
        manager[0] = nullptr;
    }
    if (manager[TCP_PORT] != nullptr) {
        delete manager[TCP_PORT];
        manager[TCP_PORT] = nullptr;
    }
}

bool PortManager::InitPort(PortBaseInitStru &portBaseInit)
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return false;
        }
        return manager[portIndex]->InitPort(portBaseInit);
    }
    return false;
}

ErrorSettingCom PortManager::InitPortConfigure(UINT stopbits, UINT flowctrl, UINT baud,
                                               QSerialPort::Parity parity, UINT databits)
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return ErrorSettingCom::ERROR_SETTING_COM_OTHER;
        }
        return manager[portIndex]->InitPortConfigure(stopbits, flowctrl, baud, parity, databits);
    }
    return ErrorSettingCom::ERROR_SETTING_COM_OTHER;
}

BOOL PortManager::ReInitPort()
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return FALSE;
        }
        return manager[portIndex]->ReInitPort();
    }
    return FALSE;
}

BOOL PortManager::StartMonitoring()
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return FALSE;
        }
        return manager[portIndex]->StartMonitoring();
    }
    return FALSE;
}

BOOL PortManager::RestartMonitoring()
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return FALSE;
        }
        return manager[portIndex]->RestartMonitoring();
    }
    return FALSE;
}

BOOL PortManager::StopMonitoring()
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return FALSE;
        }
        return manager[portIndex]->StopMonitoring();
    }
    return FALSE;
}

void PortManager::WriteToPort(LPCTSTR string)
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return;
        }
        manager[portIndex]->WriteToPort(string);
    }
}

void PortManager::WriteToPort(const char *string, int n)
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return;
        }
        manager[portIndex]->WriteToPort(string, n);
    }
}

BOOL PortManager::IsThreadAlive() const
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return FALSE;
        }
        return manager[portIndex]->IsThreadAlive();
    }
    return FALSE;
}

void PortManager::SetOneTimeReadByte(DWORD oneTimeReadByte)
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return;
        }
        manager[portIndex]->SetOneTimeReadByte(oneTimeReadByte);
    }
}

void PortManager::SetPortIndex(unsigned int index)
{
    if (index > MAX_PORT) {
        return;
    }
    portIndex = static_cast<PORT_INDEX>(index);
}

void PortManager::ClosePort() const
{
    if (portIndex <= MAX_PORT && portIndex >= MIN_PORT) {
        if (manager[portIndex] == nullptr) {
            return;
        }
        manager[portIndex]->ClosePort();
    }
}

PortBase *PortManager::GetSerialport()
{
    return manager[SERIAL_PORT];
}

PortBase *PortManager::GetTcpPort()
{
    return manager[TCP_PORT];
}
